/* LED应用程序 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

#define LED_TRIGGER "/sys/class/leds/sys-led/trigger"
#define LED_BRIGHTNESS "/sys/class/leds/sys-led/brightness"
#define USAGE()                 \
            fprintf(stderr,     \
                "usage:\n %s<on/off>\n %s<trigger><type>\n", argv[0], argv[0])

int main(int argc, char *argv[])
{
    int fd1, fd2;

    /* 校验传参 */
    if (argc < 2) {
        USAGE();
        exit(-1);
    }

    /* 打开文件 */
    fd1 = open(LED_TRIGGER, O_RDWR);
    if (0 > fd1) {
        perror("open LED_TRIGGER error");
        exit(-1);
    }

    fd2 = open(LED_BRIGHTNESS, O_RDWR);
    if (0 > fd2) {
        perror("open LED_BRIGHTNESS error");
        exit(-1);
    }

    /* 根据传参控制 LED */
    if (!strcmp(argv[1], "on")) {
        write(fd1, "none", strlen("none")); /* 先将触发模式设置为none */
        write(fd2, "1", strlen("1")); /* 点亮LED */
    } else if (!strcmp(argv[1], "off")) {
        write(fd1, "none", strlen("none")); /* 先将触发模式设置为none */
        write(fd2, "0", strlen("0")); /* 熄灭LED */
    } else if (!strcmp(argv[1], "trigger")) {
        if (3 != argc) {
            USAGE();
            exit(-1);
        }
        if (0 > write(fd1, argv[2], strlen(argv[2]))) {
            perror("write LED_TRIGGER error");
            exit(-1);
        }
    } else {
        USAGE();
        exit(-1);
    }

    /* 关闭文件 */
    close(fd1);
    close(fd2);

    exit(0);
}
